/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package casino;

import java.util.Set;

/**
 * A player that doubles his bet on every loss and resets his bet to a base amount
 * on each win. 
 * 
 * @author amaghfur
 * @since  1.0
 */
public class Player1326 extends Player {
    /** the base bet is essentially the original bet before multipliers in this betting strategy */
    protected final Rational baseBet = new Rational(10);
    /** the outcome to always bet on */
    protected final Outcome outcome = OutcomeFactory.getOutcomeFactory().createOutcome("Black", 1);
    /** the current state of the 1-3-2-6 betting system for this player */
    private Player1326State state = Player1326StateFactory.getStateFactory(this).getState("Player1326NoWins");
    
    /**
     * Class constructor for this player to play on the specified table.
     * 
     * @param tableToPlay 
     */
    public Player1326(Table tableToPlay) {
        super(tableToPlay);
    }

    /**
     * Tells this player to place bets in 1-3-2-6 fashion.
     */
    @Override
    void placeBets() {
        Bet currentBet = state.currentBet();
        Rational betAmount = currentBet.getAmountBet();
        
        if (betAmount.greaterThan(getStake())) { // end session if not enough money to follow betting strategy
            setRounds(0);
            return;
        } 
        
        try {
            getTable().placeBet(currentBet);
            setStake(getStake().minus(betAmount));
        }
        catch (InvalidBetException e) {
            System.err.println("Caught InvalidBetException: " + e.getMessage());
        }  
    }

    /**
     * Notifies this player that his bet won, update his stake to include winnings and
     * change the state of his betting strategy.
     * 
     * @param bet   his bet that win
     */
    @Override
    void win(Bet bet) {
        setStake(getStake().plus(bet.winAmount()));
        state = state.nextWon();
        System.out.println(bet.toString() + " -> won " + bet.winAmount().toString());
 
    }

    /**
     * Notifies this player that his bet lost and change the state of his betting strategy.
     * 
     * @param bet   his bet that lost
     */
    @Override
    void lose(Bet bet) {
        state = state.nextLost();
        System.out.println(bet.toString() + " -> lost " + bet.loseAmount().toString());
    }

    /**
     * Unused.
     * 
     * @param outcomes 
     */
    @Override
    void winners(Set<Outcome> outcomes) {
       
    }

    /**
     * Reset this player's original state between sessions.
     */
    @Override
    void reset() {
        state = Player1326StateFactory.getStateFactory(this).getState("Player1326NoWins");
    }
    
}
